package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.ComposePathEffect
import android.graphics.CornerPathEffect
import android.graphics.DashPathEffect
import android.graphics.Paint
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* PathEffectView05
* */
class PathEffectView05 @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val mLineStrokeWidth = context.resources.getDimension(R.dimen.dp_2)
    private val mCornerStrokeWidth = context.resources.getDimension(R.dimen.dp_5)

    private var mPathEffect: ComposePathEffect? = null
    
    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeWidth = mLineStrokeWidth
            style = Paint.Style.STROKE
            color = context.resources.getColor(R.color.color_F3A237)
        }
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2

            translate(cx, cy)

            if (mPathEffect == null) {
                val mCornerPathEffect = CornerPathEffect(mCornerStrokeWidth * 2)

                //val mDashPathEffect = DashPathEffect(floatArrayOf(cx * 11 / 12 * 4 / 2, cx * 11 / 12 * 4 / 2), cx * 11 / 12 * 4 / 10)
                //val mDashPathEffect = DashPathEffect(floatArrayOf(cx * 11 / 12 * 4 / 4, cx * 11 / 12 * 4 / 4), cx * 11 / 12 * 4 / 10)
                //val mDashPathEffect = DashPathEffect(floatArrayOf(cx * 11 / 12 * 4 / 8, cx * 11 / 12 * 4 / 8), cx * 11 / 12 * 4 / 10)
                //val mDashPathEffect = DashPathEffect(floatArrayOf(cx * 11 / 12 * 4 / 16, cx * 11 / 12 * 4 / 16), cx * 11 / 12 * 4 / 10)
                val mDashPathEffect = DashPathEffect(floatArrayOf(cx * 11 / 12 * 4 / 24, cx * 11 / 12 * 4 / 24), cx * 11 / 12 * 4 / 10)
                //val mDashPathEffect = DashPathEffect(floatArrayOf(cx * 11 / 12 * 4 / 32, cx * 11 / 12 * 4 / 32), cx * 11 / 12 * 4 / 10)

                //val mDashPathEffect = DashPathEffect(floatArrayOf(cx * 11 / 12 * 4 / 4, cx * 11 / 12 * 4 / 4), cx * 11 / 12 * 4 / 8)

                mPathEffect = ComposePathEffect(mCornerPathEffect, mDashPathEffect)
            }
            mPaint0.pathEffect = mPathEffect

            val baseX = cx * 11 / 12
            val baseY = cy * 11 / 12

            drawRect(
                -baseX,
                -baseY,
                baseX,
                baseY,
                mPaint0
            )
        }
    }
}